const FORM_TYPES = {
    SIGN: 'WRITE_SIGN',  //签名
    SEAL: 'SEAL_SIGN',  //签章
    PAGING_SEAL: 'MULTI_CHECK_MARK'
}
const CSS_UNITS = 96.0 / 72.0
const FORMS_CONFIG = [
    {
        type: FORM_TYPES.SIGN,
        name: '签名框',
        // icon: require('@images/envelopes/signname.png'),
        defaultSize: {
            height: 100*CSS_UNITS,
            width: 220*CSS_UNITS
        },
        minSize: {
            height: 60,
            width: 120
        }
    },
    {
        type: FORM_TYPES.SEAL,
        name: '签章框',
        // icon: require('@images/envelopes/seal.png'),
        defaultSize: {
            height: 119*CSS_UNITS,
            width: 119*CSS_UNITS
        },
        minSize: {
            height: 50,
            width: 50
        },
    },
    {
          type: FORM_TYPES.PAGING_SEAL,
          name: '签章框',
          // icon: require('@images/envelopes/seal.png'),
          defaultSize: {
              height: 119*CSS_UNITS,
              width: 119*CSS_UNITS
          },
          minSize: {
            height: 119*CSS_UNITS,
            width: 119*CSS_UNITS
          },
    }
]

const FORMS_CONFIG_MAP = new Map()
FORMS_CONFIG.forEach(form => FORMS_CONFIG_MAP.set(form.type, form))

class EnvelopeFormPosition {
    constructor () {
        this.ulx = 0
        this.uly = 0
        this.lrx = 0
        this.lry = 0
    }

    static formRect (top, left, width, height) {
        const position = new EnvelopeFormPosition()
        position.uly = top
        position.ulx = left
        position.lry = top + height
        position.lrx = left + width
        return position
    }
}

class FormsFactory {
  constructor () {

    }
    static createForm(formType) {
        switch (formType) {
        case FORM_TYPES.SIGN:
        case FORM_TYPES.SEAL: return new EnvelopeBaseForm()
        case FORM_TYPES.PAGING_SEAL: return new PagingSeal()
        }
    }

}

class BaseForm {
    constructor () {
        this.random = Math.floor(Math.random() * 10000000) + '' // 前端用的随机码
        /**
         * SEAL 签章
         * SIGN 签名
         * MULTI_CHECK_MARK 骑缝章
         */
        this.type = '' // 表单类型
        this.id = '' // 表单ID
        this.docId = '' // 表单所在文件ID
        this.participantWsid = '' // 参与者ID
        this.confirmed = false // 表单已被确认
        /**
         * {
         *      type: String//签署类型
         *      data: {
         *          base64:
         *      },
         *      pin: "密码"
         * }
         */
        this.certificate = null // 表单绑定的证书信息
    }

    // 是否相同
    sameAs (other) {
        if (this.type !== other.type ||
            this.id !== other.id ||
            this.docId !== other.docId ||
            this.participantWsid !== other.participantWsid ||
            JSON.stringify(this.certificate) !== JSON.stringify(other.certificate)) {
            return false
        }
        return true
    }

    fromEndData (endData) {
        if (endData.tagId) this.random = endData.tagId
        this.type = endData.formType
        this.id = endData.formWsid
        this.docId = endData.fileWsid
        this.participantWsid = endData.participantWsid

        if (endData.confirmStatus === 'CONFIRMED') {
            this.confirmed = true
        } else {
            this.confirmed = false
        }

        if (endData.initialValue) {
            this.certificate = endData.initialValue.certificate || null
        }
    }

    toEndData () {
        const data = {}
        data.tagId = this.random
        data.formType = this.type
        data.fileWsid = this.docId
        data.participantWsid = this.participantWsid
        data.initialValue = {}

        if (this.id) data.formWsid = this.id
        // if (this.certificate) data.initialValue.certificate = this.certificate

        return data
    }
}

// 信封表单基础数据对象
class EnvelopeBaseForm extends BaseForm {
    constructor () {
        super()
        this.page = 0 // 表单所在页面
        this.required = false // 是否必填
        this.scale = 1 // 缩放比例
        this.revisable = false // 是否可修改 //FIXME: 由前端控制，设置页面设为false 签名页面设置为true
        this.position = {
            top: 0,
            left: 0,
            width: 0,
            height: 0
        }
        this.data = null // 日期和文本为String 签名和签章为Object {id:, imageData}
        this.format = 'yyyy-MM-dd' // 日期格式
        this.certificate=null
        this.participantWsid=""
    }

    sameAs (other) {
        if (!super.sameAs(other) ||
            this.page !== other.page ||
            this.required !== other.required ||
            this.scale !== other.scale ||
            this.revisable !== other.revisable ||
            JSON.stringify(this.position) !== JSON.stringify(other.position) ||
            JSON.stringify(this.data) !== JSON.stringify(other.data)) {
            return false
        }
        return true
    }

    fromEndData (endData) {

        super.fromEndData(endData)

        // 类型
        switch (endData.formType) {
        case 'WRITE_SIGN':
            this.type = FORM_TYPES.SIGN
            break
        case 'SEAL_SIGN':
            this.type = FORM_TYPES.SEAL
            break
        }

        this.id = endData.formWsid
        this.participantWsid = endData.participantWsid
        this.docId = endData.fileWsid
        this.page = endData.page
        this.required = endData.required
        this.scale = endData.scale
        // let CSS_UNITS = 96.0 / 72.0
        if (endData.revisable != undefined) this.revisable = endData.revisable
        // let scale=(document.documentElement.clientWidth) / 795;
      //pt转换为px
        // console.log('end->' + this.id, endData)
        this.position = {
            top: endData.position.uly*CSS_UNITS,
            left: endData.position.ulx*CSS_UNITS,
            width: (endData.position.lrx - endData.position.ulx)*CSS_UNITS,
            height: (endData.position.lry - endData.position.uly)*CSS_UNITS
        }
      // this.position = {
      //     top: endData.position.uly,
      //     left: endData.position.ulx,
      //     width: endData.position.lrx - endData.position.ulx,
      //     height: endData.position.lry - endData.position.uly
      // }
        if (this.type === 'DATE') { // 设置日期表单日期格式类型
            this.format = endData.format ? endData.format : 'yyyy-MM-dd'
        }

        if (endData.initialValue) {
            const data = endData.initialValue
            switch (this.type) {
            case FORM_TYPES.SIGN:
                if (data.writeWsid || data.writeData) {
                    this.data = {
                        id: data.writeWsid,
                        imageData: data.writeData
                    }
                }
                break
            case FORM_TYPES.SEAL:
                if (data.sealWsid || data.sealData) {
                    this.data = {
                        id: data.sealWsid,
                        imageData: data.sealData,
                        width: data.width,
                        height: data.height
                    }
                }
                break
            }
        }
    }

    toEndData () {
        const data = super.toEndData()

        switch (this.type) {
        case FORM_TYPES.SIGN:
            data.formType = 'WRITE_SIGN'
            break
        case FORM_TYPES.SEAL:
            data.formType = 'SEAL_SIGN'
            break
        }

        data.page = this.page
        data.scale = 1
        data.required = this.required
        data.revisable = this.revisable
        data.format = this.format
        // let CSS_UNITS = 96.0 / 72.0
        // let scale=(document.documentElement.clientWidth) / 795;
        // data.position = {
        //     ulx: this.position.left*this.scale,
        //     uly: this.position.top*this.scale,
        //     lrx: (this.position.left + this.position.width)*this.scale,
        //     lry: (this.position.top + this.position.height)*this.scale
        // }
      //px转换为pt
      data.position = {
        ulx: this.position.left/CSS_UNITS,
        uly: this.position.top/CSS_UNITS,
        lrx: (this.position.left + this.position.width)/CSS_UNITS,
        lry: (this.position.top + this.position.height)/CSS_UNITS
      }

        // 数据
        if (this.data) {
            switch (this.type) {
            case FORM_TYPES.SIGN:
                data.initialValue.writeWsid = this.data.id
                data.initialValue.writeData = this.data.imageData
                data.initialValue.certType  = this.data.certType
                data.initialValue.renderingMode = this.data.renderingMode
                // data.initialValue.certificate = this.certificate
                break
            case FORM_TYPES.SEAL:
                data.initialValue.sealWsid = this.data.id
                data.initialValue.sealData = this.data.imageData
                data.initialValue.width = this.data.width
                data.initialValue.height = this.data.height
                data.initialValue.certType  = this.data.certType
                data.initialValue.renderingMode = this.data.renderingMode
                // data.initialValue.certificate = this.certificate
                break
            }
        }
        return data
    }
}

class MultiPageForm extends BaseForm {
    constructor () {
        super()
        this.direction = 'RIGHT'
        this.percent = 1
        this.margin = 0
        this.pages = 'all'
        this.certPages = '1'
    }

    sameAs (other) {
        if (!super.sameAs(other) ||
            this.direction !== other.direction ||
            this.percent !== other.percent ||
            this.margin !== other.margin ||
            this.pages !== other.pages ||
            this.certPages !== other.certPages) {
            return false
        }
        return true
    }

    fromEndData (endData) {
        super.fromEndData(endData)
        this.direction = endData.direction
        this.percent = endData.percent
        this.margin = endData.margin
        this.pages = endData.pages
        this.certPages = endData.certPages
    }

    toEndData () {
        const data = super.toEndData()
        data.direction = this.direction
        data.percent = this.percent
        data.margin = this.margin
        data.pages = this.pages
        data.certPages = this.certPages
        return data
    }
}

class PagingSeal extends MultiPageForm {
    constructor () {
        super()
        this.type = FORM_TYPES.PAGING_SEAL

        this.resizable = true
        this.resizeWidth = 119.05
        this.resizeHeight = 119.05
        this.position = {}
        this.offset = 0
        this.sealWsid = ''
        this.sealData = ''
        this.data={

        };
    }

    sameAs (other) {
        if (!super.sameAs(other) ||
            this.resizable !== other.resizable ||
            this.resizeWidth !== other.resizeWidth ||
            this.resizeHeight !== other.resizeHeight ||
            this.offset !== other.offset ||
            this.sealWsid !== other.sealWsid ||
            this.sealData !== other.sealData) {
            return false
        }
        return true
    }

    fromEndData (endData) {
        super.fromEndData(endData)
        this.offset = endData.offset*CSS_UNITS || 0
        this.resizable = endData.resizable
        this.resizeWidth = endData.resizeWidth
        this.resizeHeight = endData.resizeHeight
        this.position = {
        top: endData.position.uly,
        left: endData.position.ulx,
        width: endData.position.lrx - endData.position.ulx,
        height: endData.position.lry - endData.position.uly
        }
        if (endData.initialValue) {
          this.data.sealData=endData.initialValue.sealData;
          this.data.sealWsid=endData.initialValue.sealWsid;
        }
    }

    toEndData () {
        const data = super.toEndData()
        data.formType = 'MULTI_CHECK_MARK'
        data.offset = this.offset/CSS_UNITS
        data.resizable = this.resizable
        data.resizeWidth = this.resizeWidth
        data.resizeHeight = this.resizeHeight
        data.initialValue = {
            sealWsid: this.data.sealWsid,
            sealData: this.data.sealData,
            // certificate:this.certificate,
            certType : this.data.certType,
            renderingMode : this.data.renderingMode
        }

        return data
    }
}

function translateFormsToFront (forms) {
    // const factory = new FormsFactory()
    return forms.map(data => {
        const form = FormsFactory.createForm(data.formType)
        form.fromEndData(data)
        return form
    })
}

function translateFormsToEnd (forms) {
    return forms.map(form => {
        return form.toEndData()
    })
}

const pageSealDefaultSize = {
  width:119*CSS_UNITS,
  height:119*CSS_UNITS
}

export {
    CSS_UNITS,
    FORM_TYPES,
    FORMS_CONFIG,
    FORMS_CONFIG_MAP,
    EnvelopeFormPosition,
    EnvelopeBaseForm,
    PagingSeal,
    translateFormsToFront,
    translateFormsToEnd,
    FormsFactory,
    pageSealDefaultSize
}
